package com.web.admin.global.filter;

import com.blue.core.dict.HttpMethod;
import com.blue.http.config.ErrorConfig;
import com.blue.http.invoke.InvokerFilter;
import com.blue.http.message.HttpRequest;
import com.blue.http.message.HttpResponse;
import com.blue.http.util.HttpException;
import com.web.admin.global.util.HttpContext;

import javax.annotation.Resource;

/**
 * @author zhengjin
 * @since 1.0 2018年04月10日
 */
public class FormFilter implements InvokerFilter
{
	@Resource
	private HttpContext httpContext;

	private ErrorConfig errorConfig = ErrorConfig.getInstance();

	public FormFilter()
	{
	}

	@Override
	public boolean preHandle(HttpRequest request, HttpResponse response) throws HttpException
	{
		if (request.getHttpMethod() == HttpMethod.GET)
			return true;

		String verify = request.getPost("__VERIFY__");
		String valid = httpContext.getVerify(request);

		if (valid == null || valid.isEmpty())
			return true;

		if (verify == null || verify.isEmpty() || !verify.equalsIgnoreCase(valid))
		{
			response.setCode(ErrorConfig.ERROR_VERIFY);
			response.setMsg(errorConfig.get(ErrorConfig.ERROR_VERIFY));
			return false;
		}

		return true;
	}

	@Override
	public void afterHandle(HttpRequest request, HttpResponse response) throws HttpException
	{

	}

}
